package com.neu.envprotection.feedback.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.neu.envprotection.admin.dao.entity.User;

import com.neu.envprotection.admin.feign.UserFeign;
import com.neu.envprotection.airQuality.dao.entity.AirQuality;
import com.neu.envprotection.airQuality.feign.AirQualityFeign;
import com.neu.envprotection.common.resp.PageRespDto;
import com.neu.envprotection.common.resp.RestResp;
import com.neu.envprotection.feedback.dao.entity.Feedback;
import com.neu.envprotection.feedback.dao.mapper.FeedbackMapper;
import com.neu.envprotection.feedback.dto.req.AssignFeedbackReqDto;
import com.neu.envprotection.feedback.dto.req.GetAirQualityReqDto;
import com.neu.envprotection.feedback.dto.req.GetFeedBackReqDto;
import com.neu.envprotection.feedback.dto.resp.*;
import com.neu.envprotection.feedback.service.FeedBackService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
@Transactional
public class FeedBackServiceImpl implements FeedBackService {

//    private final AirQualityMapper airQualityMapper;
    private final FeedbackMapper feedbackMapper;
    private final UserFeign userFeign;
    private final AirQualityFeign airQualityFeign;
//    private final UserMapper userMapper;


    @Override
    public RestResp<PageRespDto<GetFeedBackRespDto>> queryFeedback(GetFeedBackReqDto condition) {
        Page page = new Page();
        page.setCurrent(condition.getPageNum());
        page.setSize(condition.getPageSize());

        List<GetFeedBackRespDto> list = feedbackMapper.queryFeedback(page,condition);

        PageRespDto<GetFeedBackRespDto> res =
                PageRespDto.of(condition.getPageNum(),condition.getPageSize(),
                        page.getTotal(),list);

        return RestResp.ok(res);
    }

    @Override
    public RestResp<PageRespDto<GetAirQualityRespDto>> queryAirQuality(GetAirQualityReqDto condition) {
        Page page = new Page();
        page.setCurrent(condition.getPageNum());
        page.setSize(condition.getPageSize());

        List<GetAirQualityRespDto> list = feedbackMapper.queryAirQuality(page,condition);

        for (int i = 0; i < list.size(); i++) {
            list.get(i).setSupervisorName(userFeign.selectById(list.get(i).getSupervisorId()).getData().getName());

        }

        PageRespDto res = PageRespDto.of(condition.getPageNum(),condition.getPageSize(),
                page.getTotal(),list);
        return RestResp.ok(res);
    }

    @Override
    public RestResp<GetFeedbackDetailRespDto> getFeedbackDetail(Integer feedbackId) {
        GetFeedbackDetailRespDto res = feedbackMapper.getDetailById(feedbackId);
        return RestResp.ok(res);
    }

    @Override
    public RestResp<GetAirQualityDetailRespDto> getAirQualityDetail(Integer airQualityId) {
        GetAirQualityDetailRespDto res = feedbackMapper.getAirDetailById(airQualityId);
        Integer supervisorId = res.getSupervisorId();
        User supervisor = userFeign.selectById(supervisorId).getData();
        res.setSupervisorName(supervisor.getName());
        res.setSupervisorTelephone(supervisor.getPhoneNumber());
        return RestResp.ok(res);
    }

    @Override
    public RestResp<Void> assignFeedbackReqDto(AssignFeedbackReqDto condition) {
        Integer feedbackId = condition.getFeedbackId();
        Integer gridManId = condition.getUserId();

        AirQuality airQuality = new AirQuality();
        airQuality.setFeedbackId(feedbackId);
        airQuality.setUserId(gridManId);
        airQuality.setCreateTime(LocalDateTime.now());
        airQuality.setIsDelete(1);
        airQualityFeign.addAirQuality(airQuality);

        Feedback feedback = feedbackMapper.selectById(feedbackId);
        feedback.setIsAssigned(1);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("id",feedbackId);
        feedbackMapper.update(feedback,wrapper);
        return RestResp.ok();
    }

    @Override
    public RestResp<GetCityCoverRespDto> getCoverData() {
        GetCityCoverRespDto dto = feedbackMapper.getCoverData();

        // 计算百分比并保留两位小数
        double provincePercentage = (dto.getProvinceCover() / 31.0) * 100;
        double cityPercentage = (dto.getCityCover() / 342.0) * 100;

        // 将百分比保留两位小数，并设置回dto对象
        dto.setProvinceCover((int) Math.round(provincePercentage * 100)/100);
        dto.setCityCover((int) Math.round(cityPercentage * 100)/100);

        // 输出结果（可选）
        System.out.println(dto);

        return RestResp.ok(dto);
    }

    @Override
    public RestResp<GetAQICountRespDto> getCountData() {
        GetAQICountRespDto dto = feedbackMapper.getCountData();
        return RestResp.ok(dto);
    }


}
